package com.kaily.imagelib.element;


import android.graphics.Bitmap;

import com.kaily.imagelib.element.ImageElement;
import com.kaily.imagelib.utils.ZxingUtils;

/**
 * 二维码生成
 *
 * @author kaily_zhou
 * @date 2018/10/19 下午3:15
 */
public class QRCodeImageElement extends ImageElement {
    private String shareUrl;
    private int width;
    private int height;
    private Bitmap defaultIcon;

    public QRCodeImageElement(String qrIconUrl, String shareUrl, Bitmap defaultIcon, boolean isRequireElement,
                              float left, float top, int width, int height) {
        super(qrIconUrl, isRequireElement, left, top);
        this.shareUrl = shareUrl;
        this.width = width;
        this.height = height;
        this.defaultIcon = defaultIcon;
    }

    @Override
    public boolean generateElement() {
        super.generateElement();
        if (bitmap != null) {
            defaultIcon = bitmap;
        }
        try {
            bitmap = ZxingUtils.createQRImage(shareUrl, width, width, defaultIcon, ZxingUtils.PADDING_SIZE_MIN);
            return true;
        } catch (Exception e) {
            return !isRequireElement;
        }

    }

}
